using Senparc.Weixin.Entities;
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.MP.AdvancedAPIs.Groups;
using Senparc.Weixin.MP.CommonAPIs;
using System;
namespace Senparc.Weixin.MP.AdvancedAPIs
{
	public static class GroupsApi
	{
		public static CreateGroupResult Create(string accessTokenOrAppId, string name, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<CreateGroupResult>(delegate(string accessToken)
			{
				string urlFormat = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token={0}";
				var data = new
				{
					group = new
					{
						name
					}
				};
				return CommonJsonSend.Send<CreateGroupResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static GroupsJson Get(string accessTokenOrAppId)
		{
			return ApiHandlerWapper.TryCommonApi<GroupsJson>(delegate(string accessToken)
			{
				string format = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token={0}";
				string url = string.Format(format, accessToken);
				return Senparc.Weixin.HttpUtility.Get.GetJson<GroupsJson>(url, null);
			}, accessTokenOrAppId, true);
		}
		public static GetGroupIdResult GetId(string accessTokenOrAppId, string openId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<GetGroupIdResult>(delegate(string accessToken)
			{
				string urlFormat = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token={0}";
				var data = new
				{
					openid = openId
				};
				return CommonJsonSend.Send<GetGroupIdResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult Update(string accessTokenOrAppId, int id, string name, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				string urlFormat = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token={0}";
				var data = new
				{
					group = new
					{
						id,
						name
					}
				};
				return CommonJsonSend.Send(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult MemberUpdate(string accessTokenOrAppId, string openId, int toGroupId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				string urlFormat = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token={0}";
				var data = new
				{
					openid = openId,
					to_groupid = toGroupId
				};
				return CommonJsonSend.Send(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult BatchUpdate(string accessTokenOrAppId, int toGroupId, int timeOut = 10000, params string[] openIds)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				string urlFormat = "https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token={0}";
				var data = new
				{
					openid_list = openIds,
					to_groupid = toGroupId
				};
				return CommonJsonSend.Send<WxJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult Delete(string accessTokenOrAppId, int groupId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				string urlFormat = "https://api.weixin.qq.com/cgi-bin/groups/delete?access_token={0}";
				var data = new
				{
					group = new
					{
						id = groupId
					}
				};
				return CommonJsonSend.Send<WxJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
	}
}
